﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace CJJCH.Web.Manage
{
    public partial class OLTypeEdit : Web.Manage.PowerBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                InitRequest();
                ReapterDataBinding();
            }
          
        }
        protected void InitRequest()
        {
            if (!string.IsNullOrEmpty(Request.QueryString["orderkey"]))
            {
                lblOrderKey.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["orderkey"].Trim());

            }
            if (!string.IsNullOrEmpty(Request.QueryString["ordertype"]))
            {
                lblOrderType.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["ordertype"].Trim());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["keyword"]))
            {
                lblKeyword.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["keyword"].Trim());
              
            }
         
            if (!string.IsNullOrEmpty(Request.QueryString["betime"]) && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["betime"].Trim()))
            {
                lblTimeBegin.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["betime"].Trim());
                
            }
            if (!string.IsNullOrEmpty(Request.QueryString["edtime"]) && LTP.Common.CheckInput.CheckDateTime(Request.QueryString["edtime"].Trim()))
            {
                lblTimeOver.Text = LTP.Common.StringClass.EncodeString(Request.QueryString["edtime"].Trim());
                
            }

            if (Request.QueryString["secondid"] != null && LTP.Common.CheckInput.CheckDigit(Request.QueryString["secondid"]))
            {

                CJJCH.Model.T_NET_MenuSecond model = new Model.T_NET_MenuSecond();
                model = new CJJCH.BLL.T_NET_MenuSecond().GetModel(Convert.ToInt32(Request.QueryString["secondid"]));
                if (model != null)
                {

                    lblSecondId.Text = LTP.Common.StringClass.EncodeString(model.ID.ToString());
                  
                    lblSecondName.Text = LTP.Common.StringClass.EncodeString(model.MenuName);
                    lnkOLType.Text = LTP.Common.StringClass.EncodeString(lblSecondName.Text)+"主题列表";
                    lblFirstID.Text = LTP.Common.StringClass.EncodeString(model.FirstID.ToString());                 
                  
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack("操作错误！");
                }
               
            }
            if (Request.QueryString["ID"] != null && LTP.Common.CheckInput.CheckGUID(Request.QueryString["ID"].Trim()))
            {
                lblGuid.Text = LTP.Common.StringClass.EncodeUrl(Request.QueryString["ID"].ToString());
            }
            else
            {
                lblGuid.Text = Convert.ToString(Guid.NewGuid());
            }
            if (!string.IsNullOrEmpty(Request.QueryString["keyWord"]))
            {
                txtOnline.Text = Request.QueryString["keyWord"].ToString();
            }
            lnkOLType.NavigateUrl = "OLTypeList.aspx?curpage=" + LTP.Common.StringClass.EncodeUrl(lblCount.Text.Trim())
                   + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                   + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                   + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                   + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                   + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                   + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim());
        }
        private void ReapterDataBinding()
        {
            CJJCH.Model.T_NET_OLType model = new Model.T_NET_OLType();

            IList<CJJCH.Model.T_NET_OLItem> list = new List<CJJCH.Model.T_NET_OLItem>();
            if (lblGuid.Text != "")
            {
                model = (new BLL.T_NET_OLType()).GetModel(new Guid(lblGuid.Text));
                txtOnline.Text = model.TypeName;
                list = (new CJJCH.BLL.T_NET_OLItem()).GetOLItem(new Guid(lblGuid.Text));
                this.rptType.DataSource = list;
                this.rptType.DataBind();
            }

        }
        protected void lbtnAdd_Click(object sender, EventArgs e)
        {

            Model.T_NET_OLItem model = new Model.T_NET_OLItem();
            string error = CheckInput();
            if (error == "")
            {
                model.ItemName = txtItem.Text;
                model.ManagerID = SessionManagerID;
                model.BuildDate = DateTime.Now;
                model.TypeID = new Guid(lblGuid.Text);
                model.ID = Guid.NewGuid();
                error = (new BLL.T_NET_OLItem()).AddOlItem(model);
                if (error == "")
                {
                    LTP.Common.WindowClass.WindowGoHref("OLTypeEdit.aspx?curpage=" + LTP.Common.StringClass.EncodeUrl(lblCount.Text.Trim())
                   + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                   + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                   + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                   + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                   + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                   + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                   + "&ID="+LTP.Common.StringClass.EncodeUrl(lblGuid.Text.Trim()));
                   
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }

            }
            else
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
        }
        protected void lbtbDel_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {

                string error = "";
                error = (new CJJCH.BLL.T_NET_OLItem()).DelOLItem((new Guid(((LinkButton)sender).CommandArgument)));
                if (string.IsNullOrEmpty(error.Trim()))
                {
                    LTP.Common.WindowClass.WindowGoHref("OLTypeEdit.aspx?scurpage=" + LTP.Common.StringClass.EncodeUrl(lblCount.Text.Trim())
                   + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                   + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                   + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                   + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                   + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                   + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                   + "&ID=" + LTP.Common.StringClass.EncodeUrl(lblGuid.Text.Trim()));
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
            else
            {
                LTP.Common.WindowClass.WindowBack("操作错误！");
            }

        }
        protected void lbtnEdit_Click(object sender, EventArgs e)
        {
            if (LTP.Common.CheckInput.CheckGUID(((LinkButton)sender).CommandArgument))
            {
                StringBuilder error = new StringBuilder();
                LinkButton linb = (LinkButton)sender;
                int Index = ((RepeaterItem)(linb.NamingContainer)).ItemIndex;
                TextBox txtTypeName = (TextBox)rptType.Items[Index].FindControl("txtTypeName");
                if (txtTypeName.Text.Trim() == "")
                {
                    error.Append("请输入条目名称！！\\n");
                }


                if (!string.IsNullOrEmpty(error.ToString()))
                {
                    LTP.Common.MessageBox.Show(this, error.ToString());
                    return;
                }
                CJJCH.Model.T_NET_OLItem model = new Model.T_NET_OLItem();
                model.ItemName = LTP.Common.StringClass.EncodeString(txtTypeName.Text.Trim());
                model.TypeID = new Guid(lblGuid.Text);
                model.ManagerID = SessionManagerID;
                model.BuildDate = DateTime.Now;
                model.ID = new Guid(((LinkButton)sender).CommandArgument);
                string res = (new BLL.T_NET_OLItem()).EditOLItem(model);
                LTP.Common.MessageBox.ShowAndRedirect(this, res, "OLTypeEdit.aspx?curpage=" + LTP.Common.StringClass.EncodeUrl(lblCount.Text.Trim())
                   + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                   + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                   + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                   + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                   + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                   + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                   + "&ID="+LTP.Common.StringClass.EncodeUrl(lblGuid.Text.Trim()));
            }
        }
        protected string CheckInput()
        {
            StringBuilder error = new StringBuilder();
            if (txtItem.Text.Trim() == "")
            {
                error.Append("请输入条目名称！！\\n");
            }
            return error.ToString();
        }
        protected void rptType_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                ((TextBox)e.Item.FindControl("txtTypeName")).Text = ((CJJCH.Model.T_NET_OLItem)e.Item.DataItem).ItemName;
                ((LinkButton)e.Item.FindControl("lbtnEdit")).CommandArgument = ((CJJCH.Model.T_NET_OLItem)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).CommandArgument = ((CJJCH.Model.T_NET_OLItem)e.Item.DataItem).ID.ToString();
                ((LinkButton)e.Item.FindControl("lbtnDel")).Attributes.Add("onclick", "javascript:if(confirm('确定要删除该数据吗？')){return true;}else{return false;}");

            }
        }
        protected void lnkEditOLType_Click(object sender, EventArgs e)
        {
            string error ="";
            if (txtOnline.Text.Trim() == "")
            {
                error="请输入主题名称！！\\n";
            }
            if (error == "")
            {
                CJJCH.Model.T_NET_OLType model = new Model.T_NET_OLType();
                model.TypeName = LTP.Common.StringClass.EncodeStr(txtOnline.Text.Trim());
                model.ManagerID = SessionManagerID;
                model.BuildDate = DateTime.Now;
                model.ID = new Guid(LTP.Common.StringClass.EncodeStr(lblGuid.Text.Trim()));
                error = (new BLL.T_NET_OLType()).EditOLType(model);
                if (error == "")
                {
                    LTP.Common.WindowClass.WindowGoHref("修改成功", "OLTypeEdit.aspx?curpage=" + LTP.Common.StringClass.EncodeUrl(lblCount.Text.Trim())
                   + "&keyword=" + LTP.Common.StringClass.EncodeUrl(lblKeyword.Text.Trim())
                   + "&orderkey=" + LTP.Common.StringClass.EncodeUrl(lblOrderKey.Text.Trim())
                   + "&ordertype=" + LTP.Common.StringClass.EncodeUrl(lblOrderType.Text.Trim())
                   + "&betime=" + LTP.Common.StringClass.EncodeUrl(lblTimeBegin.Text.Trim())
                   + "&edtime=" + LTP.Common.StringClass.EncodeUrl(lblTimeOver.Text.Trim())
                   + "&secondid=" + LTP.Common.StringClass.EncodeUrl(lblSecondId.Text.Trim())
                   + "&ID=" + LTP.Common.StringClass.EncodeUrl(lblGuid.Text.Trim()));
                   
                }
                else
                {
                    LTP.Common.WindowClass.WindowBack(error);
                }
            }
            else 
            {
                LTP.Common.WindowClass.WindowBack(error);
            }
        }
    }
}